import { Component } from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {AppService} from "../../../services/app.service";
import {NgForm} from "@angular/forms";

/**
 * Generated class for the HelpAndFeedbackPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-help-and-feedback',
  templateUrl: 'help-and-feedback.html',
})
export class HelpAndFeedbackPage {

  submitted: boolean = false;
  supportMessage: string;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public viewCtrl:ViewController,
              private appService:AppService,
              private alertCtrl:AlertController) {
  }

  ionViewDidLoad() {
  }

  /**
   * 提交
   * @param {NgForm} form
   */
  submit(form: NgForm) {
    this.submitted = true;

    if (form.valid) {
      this.supportMessage = '';
      this.submitted = false;
      this.appService.smallToast('您的反馈意见已提交','success')
    }
  }

  // If the user enters text in the support question and then navigates
  // without submitting first, ask if they meant to leave the page
  ionViewCanLeave(): boolean | Promise<boolean> {
    // If the support message is empty we should just navigate
    if (!this.supportMessage || this.supportMessage.trim().length === 0) {
      return true;
    }

    return new Promise((resolve: any, reject: any) => {
      let alert = this.alertCtrl.create({
        title: '是否确认离开？',
        message: '您当前填写的内容尚未提交，是否确认要离开页面'
      });
      alert.addButton({ text: '留下', handler: reject });
      alert.addButton({ text: '离开', role: 'cancel', handler: resolve });

      alert.present();
    });
  }

  dismiss() {
    this.viewCtrl.dismiss();
  }
}
